package com.atguigu.gulimall.cart.vo;

import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 * 需要计算的属性必须重写get方法，保证每次获取属性都会计算
 */
public class Cart {
    List<CartItem> items;

    private Integer countNum;//商品数量(所有购物项乘购买的件数之和)

    private Integer countType;//商品类型数量(购物项的数量)

    private BigDecimal totalAmount;//商品总价

    private BigDecimal reduce = new BigDecimal("0.00");//减免价格


    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        countNum = 0;
        if(items != null && items.size() > 0){
            for (CartItem item : items) {
                countNum += item.getCount();
            }
        }
        return countNum;
    }


    public Integer getCountType() {
        countType = 0;
        if(items != null && items.size() > 0){
            for (CartItem item : items) {
                countType += 1;
            }
        }
        return countType;
    }


    public BigDecimal getTotalAmount() {
        totalAmount = new BigDecimal("0");
        if(items != null && items.size() > 0){
            for (CartItem item : items) {
                totalAmount = totalAmount.add(new BigDecimal(item.getTotalPrice()+""));
            }
        }
        return totalAmount.subtract(getReduce());
    }


    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
